package ui;

import bean.GuaMap;
import cn.hutool.core.io.resource.ResourceUtil;
import components.DataFactory;
import components.MyPanel64;
import components.MyPanel8;

import javax.swing.*;
import java.awt.*;
import java.net.URL;

public class SidePanel {
    private static final SidePanel instance = new SidePanel();
    private final DataFactory dataFactory = DataFactory.getInstance();
    private SidePanel() {

    }
    public static SidePanel getInstance() {
        return instance;
    }

    JFrame frame;
    JPanel panel;
    JFrame mainFrame = MainPanel.getInstance().getFrame();
    Point parentLocation = mainFrame.getLocationOnScreen();

    public void init(String binary){
        if (frame == null){
            initFrame(binary);
            return;
        }
        refreshValues(binary);
    }

    public void initFrame(String binary){
        initPanel(binary);
        this.frame = new JFrame();
        this.frame.setSize(new Dimension(480,700));
        this.frame.setPreferredSize(new Dimension(480,700));
        this.frame.setTitle("卦象分析");
        this.frame.setContentPane(panel);
        this.frame.setResizable(false);
        this.frame.setLocation(parentLocation.x+mainFrame.getWidth(),
                parentLocation.y);
        this.frame.setLocationRelativeTo(null);
        this.frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        this.frame.setVisible(true);
    }

    public void refreshValues(String binary) {
        initPanel(binary);
        this.frame.setLocation(parentLocation.x+mainFrame.getWidth(),
                parentLocation.y);
        this.frame.setContentPane(panel);
        this.frame.setVisible(true);
    }

    private void initPanel(String binary){
        panel = new JPanel(new BorderLayout());
        JPanel parent = new JPanel(new BorderLayout());
        JPanel tmp  = binary.length() ==
                3 ? new MyPanel8(binary) : new MyPanel64(binary);
        tmp.setPreferredSize(new Dimension(tmp.getWidth(),120));
        parent.add(tmp,BorderLayout.NORTH);

        GuaMap.Gua gua = dataFactory.getGuaMap().get(binary);
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(gua.getGuaCi()).append("\n\n");
        stringBuilder.append(gua.getDaXiang()).append("\n\n");
        stringBuilder.append(gua.getTuanCi()).append("\n\n");
        stringBuilder.append(gua.getYaoCi()).append("\n");
        stringBuilder.append(gua.getYao_1()).append("\n");
        stringBuilder.append(gua.getYao_2()).append("\n");
        stringBuilder.append(gua.getYao_3()).append("\n");
        stringBuilder.append(gua.getYao_4()).append("\n");
        stringBuilder.append(gua.getYao_5()).append("\n");
        stringBuilder.append(gua.getYao_6()).append("\n\n");
        if (!"".equals(gua.getYao_7())){
            stringBuilder.deleteCharAt(stringBuilder.length() - 1);
            stringBuilder.append(gua.getYao_7()).append("\n\n");
        }

        stringBuilder.append(gua.getXiangCi()).append("\n");
        stringBuilder.append(gua.getXiaoXiang_1()).append("\n");
        stringBuilder.append(gua.getXiaoXiang_2()).append("\n");
        stringBuilder.append(gua.getXiaoXiang_3()).append("\n");
        stringBuilder.append(gua.getXiaoXiang_4()).append("\n");
        stringBuilder.append(gua.getXiaoXiang_5()).append("\n");
        stringBuilder.append(gua.getXiaoXiang_6()).append("\n");
        if (!"".equals(gua.getXiaoXiang_7())){
            stringBuilder.append(gua.getXiaoXiang_7());
        }

        JTextArea textArea = new JTextArea();
        textArea.setEditable(false);
        textArea.setLineWrap(true);
        textArea.setText(stringBuilder.toString());
        JScrollPane scrollPane = new JScrollPane(textArea,
                ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
                ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        parent.add(scrollPane,BorderLayout.CENTER);
        URL resource = ResourceUtil.getResource("yijing.jpeg");
        JLabel label = new JLabel(new ImageIcon(resource));
        label.setHorizontalAlignment(JLabel.CENTER);
        parent.add(label,BorderLayout.SOUTH);
        panel.add(parent,BorderLayout.CENTER);
        panel.setPreferredSize(new Dimension(200,400));
        panel.setSize(new Dimension(200,400));
    }



}
